#include "OutdateStrategy.h"
#include "Util.h"
#include "Cryptoer.h"
#include <Poco\File.h>
#include <Poco\Util\Application.h>
using Poco::File;
using Poco::Util::Application;

OutdateStrategy::OutdateStrategy() 
{
}

OutdateStrategy::~OutdateStrategy()
{
}

bool OutdateStrategy::needEncode(const std::string &fn)
{
	return needEncode(File(fn));
}

bool OutdateStrategy::needDecode(const std::string &fn)
{
	return needDecode(File(fn));
}



OutdateForceStrategy::OutdateForceStrategy() 
{
}

OutdateForceStrategy::~OutdateForceStrategy()
{
}

bool OutdateForceStrategy::needEncode(const Poco::File& f)
{
	return true;
}

bool OutdateForceStrategy::needDecode(const Poco::File& f)
{
	return true;
}

void OutdateForceStrategy::showInfo()
{
	Application& app = Application::instance();
	app.logger().information("Update strategy: force.\n");
}

OutdateTimeStampStrategy::OutdateTimeStampStrategy()
{
}

OutdateTimeStampStrategy::~OutdateTimeStampStrategy()
{
}

bool OutdateTimeStampStrategy::needDecode(const Poco::File& srcsf)
{
	Poco::File srcf = Util::getSrcFile(srcsf);
	if (!srcf.exists())return true;
	return (srcsf.getLastModified() > srcf.getLastModified());
}

void OutdateTimeStampStrategy::showInfo()
{
	Application& app = Application::instance();
	app.logger().information("Update strategy: timestamp.\n");
}

bool OutdateTimeStampStrategy::needEncode(const Poco::File& srcf)
{
	Poco::File srcsf = Util::getSrcsFile(srcf);
	if (!srcsf.exists())return true;
	return (srcsf.getLastModified() < srcf.getLastModified());
}

OutdateContentsStrategy::OutdateContentsStrategy()
{

}

OutdateContentsStrategy::~OutdateContentsStrategy()
{
}

bool OutdateContentsStrategy::needDecode(const Poco::File& srcsf)
{
	Poco::File srcf = Util::getSrcFile(srcsf);
	if (!srcf.exists())
		return true;
	std::string dec = Util::getCurrentCryptoer()->decodeContents(srcsf);
	std::string enc = Util::readContents(srcf);
	return dec.compare(enc) != 0;
}

void OutdateContentsStrategy::showInfo()
{
	Application& app = Application::instance();
	app.logger().information("Update strategy: contents.\n");
}

bool OutdateContentsStrategy::needEncode(const Poco::File& srcf)
{
	Poco::File srcsf = Util::getSrcsFile(srcf);
	if (!srcsf.exists())return true;
	std::string enc = Util::getCurrentCryptoer()->encodeContents(srcf);
	std::string dec = Util::readContents(srcsf);
	return dec.compare(enc) != 0;
}

OutdateMd5Strategy::OutdateMd5Strategy() 
{
}

OutdateMd5Strategy::~OutdateMd5Strategy()
{
}

bool OutdateMd5Strategy::needEncode(const Poco::File & f)
{
	return false;
}

bool OutdateMd5Strategy::needDecode(const Poco::File & f)
{
	return false;
}

void OutdateMd5Strategy::showInfo()
{
}
